#pragma once
#include <iostream>
#include <fstream>
#include <unordered_map>
#include "log.hpp"

const std::string dictname = "./dict.txt";
const std::string sep = ":";



class dict
{
public:
    dict()
    {
        std::fstream in(dictname);
        if (!in.is_open())
        {
            lg(FATAL, "ifstream open %s error", dictname.c_str());
            exit(1);
        }
        lg(INFO,"Ifstream Open Succeddful");
        std::string line;
        while (std::getline(in, line))
        {
            std::string part1, part2;
            split(line, &part1, &part2);
            mp[part1] = part2;
        }

        in.close();
        //sleep(5);

        // for(auto& a : mp)
        // {
        //     std::cout << a.second << std::endl;
        // }
    }

    bool split(std::string &s, std::string *part1, std::string *part2)
    {
        auto pos = s.find(sep);
        if (pos == std::string::npos)
            return false;
        *part1 = s.substr(0, pos);
        *part2 = s.substr(pos + 1);
        return true;
    }

    std::string translations(const std::string &key)
    {
        std::cout << key <<std::endl;
        auto iter = mp.find(key);
        if (iter == mp.end())
        {
            return "Unknow";
        }    
        else
            return iter->second;
    }

private:
    std::unordered_map<std::string, std::string> mp;
};
